/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.lieskove301.jianghongtiao.liaad.prisoner.memory;

import java.util.Deque;
import sk.lieskove301.jianghongtiao.liaad.prisoner.strategy.FightResultEnum;

/**
 * Date of create: May 15, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0515
 */
public abstract class AbstractForgetting implements Forgetting{

    /**
     * @param memory memory of agent
     * @param myBetray if counting for my betray or other agent betray
     * @param startIndex start index in memory (in case of remembering)
     * @return number of days of last betray from last index
     */
    protected int countLastBetray(Deque<FightResultEnum> memory, boolean myBetray,
            int startIndex){
        FightResultEnum[] memories = memory.toArray(new FightResultEnum[1]);
        for (int i = startIndex; i < memories.length; i++) {
            FightResultEnum fightResultEnum = memories[i];
            switch(fightResultEnum){
                case BetrayCooperate:
                    if(myBetray)  return (i - startIndex)+1;
                case CooperateBetray:
                    if(!myBetray) return (i - startIndex)+1;
                case BothBetray:  return (i - startIndex)+1;
            }
        }
        return Integer.MAX_VALUE;
    }

    /**
     * @param memory memory of agent
     * @param myCooperation if counting for my betray or other agent betray
     * @param startIndex start index in memory (in case of remembering)
     * @return number of days of last betray from last index
     */
    protected int countLastCooperation(Deque<FightResultEnum> memory, boolean myCooperation,
            int startIndex){
        FightResultEnum[] memories = memory.toArray(new FightResultEnum[1]);
        for (int i = startIndex; i < memories.length; i++) {
            FightResultEnum fightResultEnum = memories[i];
            switch(fightResultEnum){
                case CooperateBetray:
                    if(myCooperation)  return (i - startIndex) + 1;
                case BetrayCooperate:
                    if(!myCooperation) return (i - startIndex) + 1;
                case BothCooperate:    return (i - startIndex) + 1;
            }
        }
        return Integer.MAX_VALUE;
    }

    protected FightResultEnum countByResults(boolean me, boolean him){
        if(me && him) return FightResultEnum.BothCooperate;
        if(me && !him) return FightResultEnum.CooperateBetray;
        if(!me && him) return FightResultEnum.BetrayCooperate;
        if(!me && !him) return FightResultEnum.BothBetray;
        throw new IllegalArgumentException("Shit happened!");
    }

}
